
use "${source_data}\Global_Sourcing_Data.dta", clear

*isid isis_g isid_c

* 1) Winsorize values 

foreach x in pq_exp { 
capture drop p
egen p = pctile(`x') , p(97)
replace `x' = p if `x' > p
capture drop p
egen p = pctile(`x') , p(3)
replace `x' = p if `x' < p
capture drop p 
}

* 2) Picks

sort *
bys b_id HS6 dataset: gen n_bjc=_n
bys b_id HS6 year dataset: gen n_bjtc=_n

* 3) Destinations

bys b_id destination: egen pq_bd=total(pq_exp)
bys b_id: egen pq_b=total(pq_exp)
gen sh_d_b=pq_bd/pq_b 
bys b_id: egen maxsh_d_b=max(sh_d_b)
capture drop xx
gen xx=destination if maxsh_d_b==sh_d_b 
bys b_id: egen top_d_b=max(destination)

/* -------------------------------------------------------- */
/* RELATIONAL METRICS									    */
/* -------------------------------------------------------- */
capture drop num_l_bjtc
bys b_id HS6 year dataset: egen num_l_bjtc=nvals(isid_g)
capture drop yy
bys b_id HS6 year dataset: egen yy=mean(num_l_bjtc)
replace num_l_bjtc=yy if num_l_bjtc==. 

bys b_id HS6 year dataset: egen num_s_bjtc=nvals(s_id)
capture drop yy
bys b_id HS6 year dataset: egen yy=mean(num_s_bjtc)
replace num_s_bjtc=yy if num_s_bjtc==. 

capture drop sourcing_metric_bjtc
gen sourcing_metric_bjtc=-(num_s_bjtc/num_l_bjtc)

* By country-product
bys b_id HS6 year dataset: egen pq_bjtc=total(pq_exp)
bys b_id HS6_num dataset: egen pq_bjc=total(pq_exp)
capture drop weight
gen weight = pq_bjtc/pq_bjc
bys b_id HS6_num dataset: egen sourcing_metric_bjc=total((weight*sourcing_metric_bjtc)*(n_bjtc==1))

* ------------------------------------------------------------------------------ *
* TABLE B6. Summary Statistics in Global Data					 			    
* ------------------------------------------------------------------------------ *

do "${floats}\TableB6.do"

* ------------------------------------------------------------------------------ *
* TABLE C1. Sources of Variability in Sourcing Strategies				 			    
* ------------------------------------------------------------------------------ *

do "${floats}\TableC1.do"

/* -------------------------------------------------------- */
/* STANDARDIZATION FOR GRAPHS							    */
/* -------------------------------------------------------- */

capture drop xx
gen xx=sourcing_metric_bjc if n_bjc==1
bys dataset HS6_num: egen mean=mean(xx)
bys dataset HS6_num: egen sd=sd(xx)

gen SD_sourcing_metric_bjc=(sourcing_metric_bjc-mean)/sd

gen ln_pq_bjc=ln(pq_bjc)
egen gr_bjt=group(HS6_num year b_id)
egen gr_bj=group(HS6_num b_id)
bys b_id dataset: gen N_bc=_N
bys b_id dataset HS6_num: gen N_bjc=_N
bys b_id: gen N_b=_N

rename *sourcing_metric_bjc *sourcing_bjc

keep *sourcing_bjc dataset b_id gr_bj gr_bjt N_b N_bc N_bjc HS6_num  ln_pq_bjc

save "${replicated_data}\Global_for_Reshape.dta", replace

* ------------------------------------------------------------------------------ *
* FIGURE 1. Buyers' Sourcing Strategies in Different Countries				 			    
* ------------------------------------------------------------------------------ *

use "${replicated_data}\Global_for_Reshape.dta", clear

duplicates drop
reshape wide *sourcing_bjc ln_pq_bjc N_bc N_bjc, i(gr_bjt gr_bj b_id HS6_num) j(dataset) string

do "${floats}\Figure1.do"